package com.example.designmode.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体的服务对象
 */
public class ObserverClientImpl implements ObserverClient{

    //保存注册的观察者
    private List<ObserverService> list;

    //通知的信息
    private String message;

    public ObserverClientImpl(){
        list = new ArrayList<>();
    }

    @Override
    public void registerObserver(ObserverService o) {
        list.add(o);
    }

    @Override
    public void removeObserver(ObserverService o) {
        if (!list.isEmpty()) list.remove(o);
    }

    @Override
    public void notifyObserver(String message) {
        this.message = message;
        System.out.println("前台推送消息: " + message);
        list.forEach(item -> {
            item.Update(message);
        });
    }
}
